package com.samphanie.auiu.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.samphanie.auiu.admin.service.ISysRoleMenuService;
import com.samphanie.auiu.common.exception.ApiException;
import com.samphanie.auiu.mbg.mapper.SysRoleMenuMapper;
import com.samphanie.auiu.mbg.model.SysRoleMenu;
import com.samphanie.auiu.mbg.utils.PageUtils;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色菜单关联Service业务层处理
 *
 * @author zsy
 * @date 2021-05-18
 */
@Service
@RequiredArgsConstructor
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    /**
     * 查询角色菜单关联列表
     *
     * @param query
     * @return 角色菜单关联
     */
    @Override
    public List<SysRoleMenu> selectSysRoleMenuList(Map<String, String> query) {
        return baseMapper.selectSysRoleMenuList(query);
    }

    @Override
    public PageUtils listPage(Map<String, Object> query) {
        PageUtils.startPage(query);
        val list = list();
        return new PageUtils(list);
    }

    @Override
    public List<SysRoleMenu> listByRoleId(Long roleId) {
        return list(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
    }

    @Transactional
    @Override
    public boolean addRoleMenu(Long roleId, Set<Long> menus) {
        boolean result = this.deleteByRoleId(roleId);
        if (CollUtil.isEmpty(menus)) {
            return result;
        }
        Set<SysRoleMenu> roleMenus = menus.stream()
                .map(item -> SysRoleMenu.builder()
                        .roleId(roleId)
                        .menuId(item)
                        .build()).collect(Collectors.toSet());
        return saveBatch(roleMenus);
    }

    @Override
    public boolean deleteByRoleId(Long roleId) {
        return remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
    }

    @Override
    public Set<Long> listMenuIdsByRoleIds(Set<Long> roleIds) {
        List<SysRoleMenu> roleMenus = this.menuRolesByRoleIds(roleIds)
                .orElseThrow(() -> new ApiException("用户菜单不存在！"));
        if (CollUtil.isNotEmpty(roleMenus)) {
            return roleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toSet());
        }
        throw new ApiException("用户菜单不存在！");
    }

    @Override
    public Optional<List<SysRoleMenu>> menuRolesByRoleIds(Set<Long> roleIds) {
        return Optional.ofNullable(
                baseMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>()
                        .in(SysRoleMenu::getRoleId, roleIds))
        );
    }
}
